package com.test.antlr4.sql;

import com.test.antlr4.sql.bean.StatementStructure;
import com.test.antlr4.sql.core.SQLLexer;
import com.test.antlr4.sql.core.SQLParser;
import com.test.antlr4.sql.visitor.LocalSQLBaseVisitor;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;

import java.util.ArrayList;

/**
 * 测试
 *
 * @author  2021/12/23
 */
public class SqlMain {
    public static void main(String[] args) {
        String sql = "select name,age,job " +
                "from t_www left join ( " +
                "select name , age , job " +
                "from t_user  " +
                ") as a1 on sex = 2 " +
                "left join t_age on name = f1 ";
        SQLLexer mySqlLexer = new SQLLexer(CharStreams.fromString(sql));
        CommonTokenStream commonTokenStream = new CommonTokenStream(mySqlLexer);
        SQLParser mySqlParser = new SQLParser(commonTokenStream);
        SQLParser.SelectStatementContext selectStatementContext = mySqlParser.selectStatement();
        LocalSQLBaseVisitor localSQLBaseVisitor = new LocalSQLBaseVisitor();

        localSQLBaseVisitor.visit(selectStatementContext);

        System.out.println("fields------");
        localSQLBaseVisitor.getStatementStructure().getFields().forEach(System.out::println);
        System.out.println("tables------");
        localSQLBaseVisitor.getStatementStructure().getSourceTables().forEach(System.out::println);
    }
}
